<?php
class project{
	protected $id,$user,$title,$createdAt,$updatedAt;
	
	function __construct($id,$userid,$title,$createdAt,$updatedAt){
		$this->id = $id;
		$this->user = user::getUserById($userid);
		$this->title = $title;
		$this->createdAt = $createdAt;
		$this->updatedAt = $updatedAt;
	}
	
	/*GETTTERS*/
	public function getId(){
		return $this->id;	
	}
	
	public function getUser(){
		return $this->user;	
	}

	public function getTitle(){
		return $this->title;	
	}

	public function getCreatedAt(){
		return $this->createdAt;	
	}

	public function getUpdatedAt(){
		return $this->updatedAt;	
	}

	public function setUser($var){
		$this->user = $var;
	}

	public function setTitle($var){
		$this->title = $var;
	}
	
	public static function getProjectByUserId($userid){
		$conn = new dbmodifier();
		$userid = $conn->db_connection->real_escape_string($userid);
		$query = "SELECT id, user_id, title, ".
					" UNIX_TIMESTAMP(created_at) AS 'created_at',".
					" UNIX_TIMESTAMP(updated_at) AS 'updated_at' FROM project WHERE user_id =".$userid."";
		$result = $conn->updateSQL($query);
		$conn->execute();
		return $result;
	}
	
	public static function getProjectById($id){
		$conn = new dbmodifier();
		$id = $conn->db_connection->real_escape_string($id);
		$query = "SELECT id, user_id, title,".
					" UNIX_TIMESTAMP(created_at) AS 'created_at',".
					" UNIX_TIMESTAMP(updated_at) AS 'updated_at' FROM project WHERE id =".$id."";
		$result = $conn->updateSQL($query);
		$conn->execute();
		if(($result->num_rows)>0){
			$row = $result->fetch_assoc();			
			$var = new project($id,$row['user_id'],$row['title'],$row['created_at'],$row['updated_at']);
		}else{
			$var = NULL;
		}
		return $var;			
	}
	
	public static function createProject($userid,$title){
		$conn = new dbmodifier();
		$userid = $conn->db_connection->real_escape_string($userid);
		$title = $conn->db_connection->real_escape_string($title);
		$query = "INSERT INTO Project (user_id,title,created_at,updated_at) " .
				 " VALUES ('".$userid."','".$title."',FROM_UNIXTIME(".time()."),FROM_UNIXTIME(".time()."))";
		$id = $conn->updateSQLKey($query);
		$conn->execute();
		$var = new project($id,$userid,$title,time(),time());
		return $var;		
	}
	
	public function save(){
		$conn = new dbmodifier();
		$userid = $conn->db_connection->real_escape_string($this->user->getId());
		$title = $conn->db_connection->real_escape_string($this->title);
		$query = "UPDATE project SET user_id='".$userid."'";
		$query = $query.", title='".$title."'";
		$query = $query.", updated_at= FROM_UNIXTIME(".time().")";		
		$query = $query." WHERE id=".$this->id;
		$conn->updateSQL($query);
		$conn->execute();	
		$this->updated_at = time();		
	}
	
	public function delete(){
		$conn = new dbmodifier();
		$query = "DELETE FROM project WHERE id=".$this->id;
		$result = $conn->updateSQL($query);
		$conn->execute();
		return $result;		
	}
}
?>